package com.lala.rennigou.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lala.rennigou.mapper.EasybuyNewsMapper;
import com.lala.rennigou.pojo.EasybuyNews;
import com.lala.rennigou.service.EasybuyNewsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class EasybuyNewsController {
    @Autowired
    private EasybuyNewsServiceImpl easybuyNewsService;


    @RequestMapping(value = "/zixun")
    public Object selectByTime(){
        List<EasybuyNews> list = easybuyNewsService.selectByTime();
        //System.out.println(list);
        return JSONArray.toJSON(list);
    }

    @RequestMapping(value = "/newsfenye")
    public PageInfo<EasybuyNews> Newsfenye(int pagenum){
        return easybuyNewsService.fenye(pagenum,5);
    }

    @RequestMapping(value = "/addzixun")
    public Object addzixun(EasybuyNews easybuyNews){
        Date date = new Date();
        String dateStr =new SimpleDateFormat("yyyy-MM-dd").format(date);
        easybuyNews.setCreatetime(dateStr);
        int a = easybuyNewsService.addzixun(easybuyNews);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }

    @RequestMapping(value = "/deletezixun")
    public Object deletezixun(int id){
        int a = easybuyNewsService.deletezixun(id);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }

    @RequestMapping(value = "/updatezixun")
    public Object updatezixun(EasybuyNews easybuyNews){
        int a = easybuyNewsService.updatezixun(easybuyNews);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }

    @RequestMapping(value = "/newsByTT")
    public Object newsByTT(String title){
        int a = easybuyNewsService.newsByTT(title);
        if(a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }



}
